import sqlite3

con = sqlite3.connect("./data/user/user.db", check_same_thread=False)
user_cur = con.cursor()


def check_username_and_password(username, password):
    """
    检查用户的账户和密码
    """
    re1 = user_cur.execute(f"SELECT password FROM user WHERE username='{username}';")
    for i in re1:
        cur_password = i[0]
        if password == cur_password:
            return True
        return False
    return False


def save_username_and_password(username, password):
    """
    保存用户的账户和密码
    """
    user_cur.execute(
        f"SELECT EXISTS(SELECT 1 FROM user WHERE username = '{username}');"
    )
    for i in user_cur:
        if i[0] == 1:
            return False
        else:
            user_cur.execute(f"INSERT INTO user VALUES ('{username}','{password}');")
            con.commit()
            return True
    return False
